"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容M*数据库subdate函数
Case Name   : subdate函数，第一个参数为time类型，time输入格式测试
Create At   : 2023.01.06
Owner       : z@wan005
Description :
    1、第一个参数的time类型为hhmmss
    2、第一个参数的time类型为'hhmmss'
    3、第一个参数的time类型为'hh:mm:ss'
    4、第一个参数为原始time类型
    5、清理环境
Expect      :
    1、弹出warning并返回null
    2、弹出warning并返回null
    3、弹出warning并返回null
    4、成功
    5、成功
History     :modified by z@wan005，2023.09.26，select场景不报错，修改断言
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest06(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_subdate_0006"

    def test_subdate(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '--step1:第一个参数的time类型为hhmmss;expect:弹出warning并返回null--'
        self.log.info(text)
        sql_cmd = "select subdate(8385959, interval 1 hour);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn('WARNING:  date/time field value out of range: "8385959',
                      sql_res, '执行失败' + text)
        self.assertIn('subdate \n---------\n \n(1 row)', sql_res,
                      '执行失败' + text)

        text = "-step2:第一个参数的time类型为'hhmmss';expect:弹出warning并返回null-"
        self.log.info(text)
        sql_cmd = "select subdate('8385959', interval 1 hour);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn('subdate \n---------\n \n(1 row)', sql_res,
                      '执行失败' + text)

        text = "step3:第一个参数的time类型为'hh:mm:ss';expect:弹出warning并返回null"
        self.log.info(text)
        sql_cmd = "select subdate('838:59:59', interval 1 hour);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertIn('subdate \n---------\n \n(1 row)', sql_res,
                      '执行失败' + text)

        text = "--step4:第一个参数为原始time类型;expect:成功--"
        self.log.info(text)
        sql_cmd = "select subdate(time'838:59:59', interval 1 hour);" \
                  "select subdate(time'8385959', interval 1 hour);" \
                  "select subdate(8385959::time, interval 1 hour);" \
                  "select subdate(cast(8385959 as time), interval 1 hour);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 19, '行数不到19行')
        self.assertEqual('837:59:59', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('837:59:59', sql_res.splitlines()[7].strip(),
                         '执行失败' + text)
        self.assertEqual('837:59:59', sql_res.splitlines()[12].strip(),
                         '执行失败' + text)
        self.assertEqual('837:59:59', sql_res.splitlines()[17].strip(),
                         '执行失败' + text)

    def tearDown(self):
        text = '----step5:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
